package router

import (
	"SecKill/common/middlewares"
	"SecKill/service"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func Router() *gin.Engine {
	r := gin.Default()
	store := cookie.NewStore([]byte("secret"))
	r.Use(sessions.Sessions("mysession", store))
	r.Use(middlewares.AuthToken())
	r.GET("/hi", service.Hi)
	user := r.Group("/user")
	{
		user.POST("/login", service.Login)
		user.POST("/code", service.SendCode)
		user.GET("/me", middlewares.AuthUserCheckByRedis(), service.Me)
	}
	blog := r.Group("/blog")
	{
		blog.POST("", service.SaveBlog)
		blog.GET("/like/:id", service.LikeBlog)
		blog.GET("/of/me", service.QueryMyBlog)
		blog.GET("/hot", service.QueryHotBlog)
	}
	shop := r.Group("/shop")
	{
		shop.GET("/:id", service.GetShopByIdLogicalExpire)
		shop.POST("", service.SaveShop)
		shop.PUT("", service.UpdateShop)
		shop.GET("/of/type", service.GetShopByType)
		shop.GET("/of/name", service.GetShopByName)
	}
	shopType := r.Group("/shop-type")
	{
		shopType.GET("/list", service.QueryTypeList)
	}
	voucher := r.Group("/voucher")
	{
		voucher.POST("", service.AddVoucher)
		voucher.POST("/seckill", service.AddSeckillVoucher)
		voucher.GET("/list/:shopId", service.QueryVoucherOfShop)
	}
	voucher_order := r.Group("/voucher-order")
	{
		voucher_order.POST("/seckill/:id", middlewares.AuthUserCheckByRedis(), service.AddVoucherOrderByLuaV2)
	}
	return r
}
